﻿package soundCom
{
	import debugs.Debug;
	import flash.display.MovieClip;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	import media.sound;
	import media.soundItem;
	import flash.events.Event;
	import net.netvars;
	import src.libCom.FixLib;
	import media.fixSound;
	
	/**
	 * ...
	 * @author keys_star@163.com
	 */
	public class Scramble extends MovieClip
	{
		private var soundItems:Array;
		private var timers:Timer;
		private var count:uint = 30;
		private var systime:Timer;
		public var ID:uint = 0;
		private var heartSound:sound;
		public function Scramble() 
		{
		}
		public function init() { 
			gotoAndStop(1);
			soundItems = new Array;
			var u:String = "sound/";
			soundItems.push(u + "ring");
			soundItems.push(u + 7);
			var idcard:Number = Number(netvars.getVar("IDCARD"));
			var sa:Array = fixSound.getIDCardSound(idcard,"sound");
			soundItems = soundItems.concat(sa);
			soundItems.push(u + 8);
			soundItems.push(u + 9);
			soundItems.push(u + 10);
		}
		public function plays() {
			gotoAndStop(2);
			var cd:Number = Number(netvars.getVar("IDCARD"));
			var idc = getChildByName("idcard");
			idc.text = cd;
			var si:soundItem = new soundItem;
			si.addEventListener(Event.SOUND_COMPLETE, scom);
			si.soundArr = soundItems;
			si.play();
		}
		private function scom(e:Event):void {
			gotoAndStop(3);
			CountdownStart();
			playHeart();
		}
		private function playHeart() {
			heartSound = new sound("sound/heart.mp3");
			heartSound.addEventListener(Event.SOUND_COMPLETE, heartRun);
			heartSound.play()
		}
		private function heartRun(e:Event) {
			heartSound.play();
		}
		private function CountdownStart() {
			count = 30;
			timers = new Timer(1000, count);
			timers.addEventListener(TimerEvent.TIMER, timerun);
			timers.addEventListener(TimerEvent.TIMER_COMPLETE, timecom);
			timers.start();
			
			var s=netvars.getVar("LIBID").toString();
			var id = Number(netvars.getVar("ID"));
			var u = "QALIB/LIB" + id + "/sound/";
			
			var si:soundItem = new soundItem;
			si.addEventListener(Event.SOUND_COMPLETE, ascom);
			si.soundArr = [u+s];
			si.play();
		}
		private function ascom(e:Event) {
			timecom(null);
		}
		private function timerun(e:TimerEvent) {
			var num_txt = getChildByName("num_txt") as TextField;
			num_txt.text = --count;
		}
		private function timecom(e:TimerEvent) {
			timers.stop();
			systime.reset();
			
			heartSound.removeEventListener(Event.SOUND_COMPLETE, heartRun);
			heartSound.stop();
			
			gotoAndStop(1);
			
			var si:soundItem = new soundItem;
			si.addEventListener(Event.SOUND_COMPLETE, acom);
			si.soundArr = ["sound/"+11];
			si.play();
		}
		private function acom(e:Event) {
			dispatchEvent(new Event(Event.COMPLETE));
		}
		public function systimeStart() {
			Debug.send(this + ".sounditem.start");
			systime = new Timer(1 * 60 * 1000,1);
			systime.addEventListener(TimerEvent.TIMER_COMPLETE, stcom);
			systime.start();
		}
		private function stcom(e:TimerEvent) {
			Debug.send(this + ".start");
			plays();
			dispatchEvent(new Event(Event.CHANGE));
		}
		public function stops() {
			try{systime.stop()}catch(e:Error){}
		}
	}
}